package com.ews.onlineexchange.controller;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.enterprise.inject.Model;
import javax.inject.Inject;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import com.ews.onlineexchange.model.*;
import org.apache.commons.lang3.StringUtils;

import com.ews.onlineexchange.controller.pub.QualificationConditionBean;
import com.ews.onlineexchange.service.EntityQuery;
import com.ews.onlineexchange.service.UserService;
import com.ews.onlineexchange.util.DateUtil;

@Model
public class SuccessBulletinList extends EntityQuery<Bulletin> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Date startDate;
	private Date endDate;
	private Integer year;
	public SuccessBulletinList() {
		setMaxResults(MAX_RESULTS_NORMAL);
		setOrder("overTime desc");
	}
	@Inject
	UserService userService;
	
	@Inject
	QualificationConditionBean conditionBean;
	@Override
	protected List<Predicate> getRestrictions(CriteriaBuilder cb, Root<Bulletin> entity) {
		List<Predicate> restrictions = new ArrayList<>();
		restrictions.add(cb.equal(entity.get("delFlag"), DataEntity.DEL_FLAG_NORMAL));
		Join<Bulletin, ServiceBuyer> buyer = entity.join("buyer");
		//根据县区过滤
		super.addBulletinRegionRestriction(restrictions,buyer,cb);
		restrictions.add(cb.isTrue(entity.<Boolean>get("over")));	
		restrictions.add(cb.equal(entity.get("tradeStatus"), TradeStatus.Agreed));
		restrictions.add(cb.isFalse(entity.<Boolean>get("cancelled")));
		String projectName = (String)getParameterValues().get("projectName");
		if (!StringUtils.isBlank(projectName))
			restrictions.add(cb.like(entity.get("projectName"), getStringRestriction(projectName)));
		if(startDate != null) {
			restrictions.add(cb.greaterThanOrEqualTo(entity.get("overTime"), startDate));
		}
		if(endDate != null) {
			restrictions.add(cb.lessThanOrEqualTo(entity.get("overTime"), endDate));
		}
		
		Join<Object, Object> qualificationTemplates = null;
		if (!StringUtils.isBlank(conditionBean.getQualificationClassId())) {
			if(qualificationTemplates == null )qualificationTemplates = entity.join("qualificationGroup").join("groups").join("qualificationTemplates");
			Join<QualificationTemplate, QualificationClass> clazz = qualificationTemplates.join("qualificationClass");
			restrictions.add(cb.equal(clazz.get("id"), conditionBean.getQualificationClassId()));
		}
		
//		if (conditionBean.getCertLevel() != null) {
//			Join<QualificationTemplate, QualificationLevel> level = qualificationTemplates.join("qualificationLevel");
//			restrictions.add(cb.equal(level.get("value"), conditionBean.getCertLevel()));
//		}
		Join<QualificationClass, Sector> sector = null;
		if (!StringUtils.isBlank(conditionBean.getSectorId()) ) {
			Join<QualificationTemplate, QualificationClass> clazz = qualificationTemplates.join("qualificationClass");			
			sector = clazz.join("rootSectors");	
			restrictions.add(cb.equal(sector.get("id"), conditionBean.getSectorId()));
		}
		if(!StringUtils.isBlank(conditionBean.getChildSectorSno())) {
			sector = qualificationTemplates.join("sectors");	
			restrictions.add(cb.like(sector.get("sno"), getStringRestriction(conditionBean.getChildSectorSno())));
		}
		
		if(year != null) {
			Date firstDayOfYear = DateUtil.getFirstDayOfYear(year);
			Date lastDayOfYear = DateUtil.getLastDayOfYear(year);
			restrictions.add(cb.greaterThanOrEqualTo(entity.get("createDate"), firstDayOfYear));
			restrictions.add(cb.lessThanOrEqualTo(entity.get("overTime"), lastDayOfYear));
			
		}
		
		return restrictions;
	}
	public Date getStartDate() {
		return startDate;
	}
	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}
	public Date getEndDate() {
		return endDate;
	}
	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}
	public Integer getYear() {
		return year;
	}
	public void setYear(Integer year) {
		this.year = year;
	}
//	@Override
//	protected void fetch(Root<Bulletin> entity) {
//		
//		super.fetch(entity);
//		entity.fetch("trades",JoinType.LEFT);
//	}
//	
	

	
	

}
